package com.itboy.controller;

import com.itboy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.DispatcherServlet;
import sun.plugin.com.DispatchClient;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    //发现客户端
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/find/{id}")
    public User findById(@PathVariable Integer id){
        //返回值类型是集合  获取服务者的实例 参数写服务者配置文件中的名字
        List<ServiceInstance> instanceList = discoveryClient.getInstances("provice-service");
        ServiceInstance serviceInstance = instanceList.get(0);
        URI uri = serviceInstance.getUri();
        String url =uri+"/user/findById/" + id;
        User user = restTemplate.getForObject(url,User.class);
        return user;
    }
    @GetMapping("/getservice")
    public List<ServiceInstance> server(){
        //返回值类型是集合  获取服务者的实例 参数写服务者配置文件中的名字
        List<ServiceInstance> instanceList = discoveryClient.getInstances("provice-service");
        return instanceList;
    }
}
